from django.db.models import F
from django.shortcuts import render
from django.views import View

from account.models import MyUser
from article.models import ArticleTag, Comment, ArticleInfo
from boke.constants import ALBUM_PAGE_NEWS_COUNT, ALBUM_PAGE_WALL_COUNT, COMMENTS_PAGE_WALL_COUNT, \
    TADGS_PAGE_WALL_COUNT, PER_PAGE_NEWS_COUNT
from .models import AlbumInfo
from django.core.paginator import Paginator
from django.core.paginator import PageNotAnInteger,EmptyPage
# Create your views here.
def base(request):
    #文章
    news_queryset = ArticleInfo.objects.values('id', 'title', 'content', 'articlephoto', 'reading',
                                               'update_time', ).annotate(
        tag_name=F('article_tag__tag'), author=F('author__username'))
    # 过滤
    news=news_queryset.filter(is_delete=False)
    # 分页
    ArticleList = Paginator(news, PER_PAGE_NEWS_COUNT)
    Articles=ArticleList.page(1)
    #分类
    tags = ArticleTag.objects.only('id', 'tag').filter(is_delete=False)
    tagList=Paginator(tags,TADGS_PAGE_WALL_COUNT)
    taglists=tagList.page(1)
    #图片
    albumList = AlbumInfo.objects.all()
    # 进行分页处理
    paginators = Paginator(albumList, ALBUM_PAGE_WALL_COUNT)
    paginator_s=paginators.page(1)
    # 评论
    Comments_page_LIST = Comment.objects.filter(is_delete=False)
    Commentss=Paginator(Comments_page_LIST, COMMENTS_PAGE_WALL_COUNT)
    Comments = Commentss.page(1)
    return render(request,'base.html',locals())

def album_page(request):
    # 文章
    news_queryset = ArticleInfo.objects.values('id', 'title', 'content', 'articlephoto', 'reading',
                                               'update_time', ).annotate(
        tag_name=F('article_tag__tag'), author=F('author__username'))
    # 过滤
    news = news_queryset.filter(is_delete=False)
    # 分页
    ArticleList = Paginator(news, PER_PAGE_NEWS_COUNT)
    Articles = ArticleList.page(1)
    # 分类
    tags = ArticleTag.objects.only('id', 'tag').filter(is_delete=False)
    tagList = Paginator(tags, TADGS_PAGE_WALL_COUNT)
    taglists = tagList.page(1)
    # 图片
    albumList = AlbumInfo.objects.all()
    # 进行分页处理
    paginators = Paginator(albumList, ALBUM_PAGE_WALL_COUNT)
    paginator_s = paginators.page(1)
    # 评论
    Comments_page_LIST = Comment.objects.filter(is_delete=False)
    Commentss = Paginator(Comments_page_LIST, COMMENTS_PAGE_WALL_COUNT)
    Comments = Commentss.page(1)
    albumList = AlbumInfo.objects.all()
    # 进行分页处理
    paginator = Paginator(albumList, ALBUM_PAGE_NEWS_COUNT)
    return render(request, 'album.html', locals())

def album_user(request,name,page):
    '''
    对用户图片墙进行查询
    :param request:
    :param name:
    :param page:
    :return:
    '''
    user=MyUser.objects.get(username='admin')
    albumList=AlbumInfo.objects.only('id', 'user_id').filter(user_id=user)
    if albumList:
        #进行分页处理
        paginator=Paginator(albumList,ALBUM_PAGE_NEWS_COUNT)
        try:
            pageInfo=Paginator.page(page)
        except PageNotAnInteger:
            #如果参数page的数据类型不是整型，就只能返回第一页数据
            pageInfo=Paginator.page(1)
        except EmptyPage:
            #若访问的页数大于实际页数，则返回最后一页数据
            pageInfo = Paginator.page(Paginator.num_pages)
        return render(request, 'album.html', locals())
    else:
        return render(request,'album.html')

class AlbumListView(View):
    """
    新闻列表视图
    url: /album_page/
    args: tag, page
    """
    def get(self, request):
        albumList = AlbumInfo.objects.all()